import 'package:basic_common/app_theme.dart';
import 'package:basic_common/common_exports.dart';
import 'package:basic_widgets/app_bar_widget.dart';
import 'package:basic_widgets/text_widget.dart';
import 'message_controller.dart';

class MessagePage extends GetView<MessageController> {
  const MessagePage({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: mainAppbar(title: "消息"),
      body: SingleChildScrollView(
        physics: const AlwaysScrollableScrollPhysics(),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Obx(() => Container(
                  // height: ScreenHelper.height(300),
                  child: SingleChildScrollView(
                    physics: AlwaysScrollableScrollPhysics(),
                    child: ExpansionPanelList(
                      elevation: 0,
                      // 点击折叠按钮实现面板的伸缩
                      expansionCallback: (int panelIndex, bool isExpanded) {
                        if (panelIndex == 0) {
                          controller.isExpanded.value = !isExpanded;
                        }
                        if (panelIndex == 1) {
                          controller.isSystemExpanded.value = !isExpanded;
                        }
                        if (panelIndex == 2) {
                          controller.isPatientExpanded.value = !isExpanded;
                        }
                      },
                      children: [
                        msgPlane(
                            icon: "assets/message/icon_server.png",
                            title: '服务消息',
                            subMsg: '签约消息，订单消息，预约消息',
                            isExpaned: controller.isExpanded.value,
                            mslList: controller.serverList),
                        msgPlane(
                            icon: "assets/message/icon_system.png",
                            title: '系统消息',
                            subMsg: '评价通知',
                            isExpaned: controller.isSystemExpanded.value,
                            mslList: controller.systemList),
                      ],
                    ),
                  ),
                )),
            divider(),
            GestureDetector(
              onTap: () => Get.toNamed(AppRoutes.patientMsgList),
              child: patientWidget(
                  icon: "assets/message/icon_patient.png",
                  title: '患者消息',
                  subMsg: '患者咨询沟通消息'),
            )
            // Text("测试")
          ],
        ),
      ),
    );
  }

  /// 患者消息

  Widget patientWidget(
      {required icon, required String title, required String subMsg}) {
    return Container(
      color: Colors.white,
      height: ScreenHelper.height(90),
      padding: EdgeInsets.symmetric(horizontal: ScreenHelper.width(20)),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Container(
            width: ScreenHelper.width(50),
            height: ScreenHelper.height(50),
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage(icon, package: "basic_common"))),
          ),
          SizedBox(
            width: ScreenHelper.width(20),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                "患者消息",
                style: TextStyle(
                    color: AppTheme.dartTextColor,
                    fontSize: ScreenHelper.sp(16)),
              ),
              SizedBox(
                height: ScreenHelper.height(10),
              ),
              Text(
                "患者沟通消息",
                style: TextStyle(
                    color: AppTheme.seconddarkWeakTextColor,
                    fontSize: ScreenHelper.sp(14)),
              )
            ],
          ),
          Expanded(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                SizedBox(
                  width: ScreenHelper.width(10),
                ),
                Image.asset(
                  "assets/common/icon_arrow_next.png",
                  width: ScreenHelper.width(18),
                  package: "basic_common",
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  /// 消息面板

  ExpansionPanel msgPlane(
      {required String icon,
      required String title,
      required String subMsg,
      required bool isExpaned,
      required List mslList}) {
    return ExpansionPanel(
      headerBuilder: (BuildContext context, bool isExpanded) {
        return Container(
          padding: EdgeInsets.all(4.0),
          child: ListTile(
            minLeadingWidth: 6,
            leading: Container(
              width: ScreenHelper.width(50),
              height: ScreenHelper.height(50),
              decoration: BoxDecoration(
                  image: DecorationImage(
                      image: AssetImage(icon, package: "basic_common"))),
            ),
            title: Text(
              title,
              style: TextStyle(color: AppTheme.dartTextColor),
            ),
            subtitle: Text(
              subMsg,
              style: TextStyle(fontSize: ScreenHelper.sp(13)),
            ),
          ),
        );
      },
      body: Container(
        padding: EdgeInsets.all(4.0),
        width: double.infinity,
        child: ListView.separated(
            shrinkWrap: true,
            physics: AlwaysScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              Map item = mslList[index];
              return GestureDetector(
                  onTap: () => Get.toNamed(AppRoutes.msgDetail,
                      arguments: {'msgTitle': item['title'], "title": title}),
                  child: buildListItem(
                      icon: icon,
                      title: item['title'],
                      subTitle: item['msg'],
                      time: item['time']));
            },
            separatorBuilder: (context, index) => divider(),
            itemCount: mslList.length),
      ),
      isExpanded: isExpaned, // 设置面板的状态，true展开，false折叠
    );
  }

  Widget buildListItem({
    required String title,
    required String subTitle,
    required String time,
    required String icon,
  }) {
    return ListTile(
      minLeadingWidth: 6,
      leading: Container(
        width: ScreenHelper.width(48),
        height: ScreenHelper.height(48),
        decoration:
            BoxDecoration(image: DecorationImage(image: AssetImage(icon))),
      ),
      title: Text(
        "${title}",
        style: TextStyle(
            color: AppTheme.dartTextColor, fontSize: ScreenHelper.sp(16)),
      ),
      subtitle: Text(
        "${subTitle}",
        style: TextStyle(fontSize: ScreenHelper.sp(14)),
      ),
      trailing: Text(
        "${time}",
        style: TextStyle(
          color: AppTheme.weakTextColor,
          fontSize: ScreenHelper.sp(13),
        ),
      ),
    );
  }
}
